# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

#双重认证问题
# before_all_do
#   ENV["FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"] = "mawl-wqfs-ltbd-jpqu"
# end

platform :ios do
  desc "Push a new beta build to TestFlight"

    # 所有lane执行之前
    # 使用环境变量提供这个密码给fastlane，解决双重认证生成的特殊密码
    # http://www.zyiz.net/tech/detail-99970.html
    before_all do
    ENV["FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"] = "mawl-wqfs-ltbd-jpqu"
    end

  lane :Apple_Store do

    increment_build_number(xcodeproj: "DXGApp.xcodeproj")
    gym(
        scheme: 'DXGApp',
        # 指定打包方式 ["app-store", "ad-hoc", "package", "enterprise", "development", "developer-id"]
        #teamID: "GK63F24GQ2",  # developer.apple.com 上查看
        export_method: "app-store",
        configuration: "Apple_Store",        # 指定打包配置，对应XCode的configuration
        xcargs: "-allowProvisioningUpdates",
        output_directory:"./fastlane/build", # 指定输出文件夹
        output_name: 'DXGApp_Apple_Store.ipa'  # 指定输出ipa名字
    )

    upload_to_testflight(
      ipa:"./fastlane/build/DXGApp_Apple_Store.ipa",
      skip_waiting_for_build_processing:true
      #skip_submission: true
      # demo_account_name: "1213618667@qq.com",
      # demo_account_password: "ChenMan8807@"
    )
    # pilot(ipa:"./fastlane/build/DXGApp_Apple_Store.ipa",skip_waiting_for_build_processing:true) #管理TestFlight的测试用户，上传二进制文件
    # upload_to_testflight
  end

  # 上传到iTune connect   test flight

  # 打包ipa: Debug 
  # 查看项目配置为 
  # Team: Shanghai Yingma Computer Network Technology Co., Ltd.
  # Signing Certificate: Apple Development)
  # Bundle Identifier: com.rjhy.gliese
  lane :Debug_ipa do
    desc "打包Development -> ipa"
    #increment_build_number(xcodeproj: "DXGApp.xcodeproj")
    build_app(
      scheme: 'DXGApp',
      # Method used to export the archive. Valid values are: app-store, validation, ad-hoc, package, enterprise, development, developer-id and mac-application
      export_method: "development", # 指定打包方式 ["app-store", "ad-hoc", "package", "enterprise", "development", "developer-id"]
      #teamID: "GK63F24GQ2",  # developer.apple.com 上查看
      xcargs: "-allowProvisioningUpdates",
      configuration: "Debug",
      # provisioningProfiles: { 
      #   "com.rjhy.gliese" => "Provisioning Profile Name",
      #   "com.example.bundleid2" => "Provisioning Profile Name 2",
      #   "com.example.bundleid2" => "也可以指定字符串",
      # }
      output_directory: './fastlane/build/',
      output_name: 'DXGApp_Debug.ipa'
    )
  end

  # https://cloud.tencent.com/developer/ask/209399
  lane: learn
    version = get_version_number
    #build = increment_build_number
    print version
  end

  # 打包ipa: Debug_Test 
  # 查看项目配置为 
  # Team: Shanghai Yingma Computer Network Technology Co., Ltd.
  # Signing Certificate: Apple Development)
  # Bundle Identifier: com.rjhy.gliese
  # Singing Certificate: Apple Distribution: Shanghai Yingma Computer...
  lane :Debug_Test_ipa do
    desc "打包 ad-hoc -> ipa"
    #match(type: "adhoc")
    #install_provisioning_profile(path: "profiles/profile.mobileprovision")
    build_app(
      scheme: 'DXGApp',
      export_method: "ad-hoc",
      # export_options: {
      #   provisioningProfiles: {
      #     "com.rjhy.gliese" => "hyc_adhoc_profile"
      #   }
      # },
      output_directory: './fastlane/build/',
      configuration: "Debug_Test",
      output_name: 'DXGApp_Debug_Test.ipa'
    )
  end

  # https://stackoverflow.com/questions/42960644/how-to-get-itunes-connect-team-id-and-team-name
  # 运行脚本后查看 ./fastlane/ItunesTeamNames
  # Shanghai New Winner investment consultants Co., Ltd. (120035784)||Shanghai Yingma Computer Network Technology Co., Ltd. (122447584)
  lane :getTeamNames do
    require "spaceship" 
    # clientTunes = Spaceship::Tunes.login("用户名，形如：1213618667@qq.com", "密码")
    # client = Spaceship::Portal.login("用户名，形如：1213618667@qq.com", "密码")
    client = Spaceship::Portal.login("1213618667@qq.com", "ChenMan8807@")
    clientTunes = Spaceship::Tunes.login("1213618667@qq.com", "ChenMan8807@")

    tb = Terminal::Table.new
    tb.title = "team_id (Developer Portal Team ID)"
    tb.headings = ["name", "team_id"]
    client.teams.each do |team|
        tb.add_row(["#{team['name']}", "#{team['teamId']}"])
    end
    print tb

    print("\n\n")

    tb = Terminal::Table.new
    tb.title = "itc_team_id (App Store Connect Team ID)"
    tb.headings = ["name", "itc_team_id"]
    clientTunes.teams.each do |team|
        tb.add_row(["#{team['contentProvider']['name']}", "#{team['contentProvider']['contentProviderId']}"])
    end
    print tb
  end
end